﻿using System;
using System.Diagnostics.Contracts;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Reflection;
using System.Net.Http;
using Microsoft.Extensions.Options;
using WxLib.Core.Util;
using WxLib.Core.Serializers;
using WxLib.Core.RequestHandlers;
using WxLib.Pay;
using WxLib.Common;
using WxLib;

namespace wxLib.Pay
{
    /// <summary>
    /// h5支付
    /// </summary>
    public class H5Pay
    {
        /// <summary>
        /// 获取H5方式的手机微信支付地址，异常返回""
        /// </summary>
        /// <param name="out_trade_no">订单号</param>
        /// <param name="body">描述</param>
        /// <param name="total_fee">总价</param>
        /// <param name="ip">客户IP</param>
        /// <param name="isCustomerOrder">是否用户订单</param>
        /// <returns></returns>
        public async Task<string> GetWxH5PayUrlAsync(string out_trade_no, string body, string total_fee, string ip, bool isCustomerOrder = false)
        {
            WxApiOptions wxPayConfig = new WxApiOptions();
            Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler();
            #region 构造请求参数
            packageReqHandler.SetParameter("appid", wxPayConfig.AppId);//APPID
            packageReqHandler.SetParameter("mch_id", wxPayConfig.MchId);//商户号
            packageReqHandler.SetParameter("nonce_str", Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr());
            packageReqHandler.SetParameter("body", body);
            packageReqHandler.SetParameter("out_trade_no", out_trade_no);//订单号
            packageReqHandler.SetParameter("total_fee", total_fee); //金额,以分为单位
            packageReqHandler.SetParameter("spbill_create_ip", ip);//IP
            if (isCustomerOrder)
            {
                packageReqHandler.SetParameter("notify_url", "https://www.qierge.com/wx/OrderResultNotifyAsync"); //回调地址
            }
            else
            {
                packageReqHandler.SetParameter("notify_url", "https://www.qierge.com/wx/ResultNotifyAsync"); //回调地址
            }
            packageReqHandler.SetParameter("trade_type", "MWEB");//这个不可以改。固定为Mweb
            packageReqHandler.SetParameter("sign", packageReqHandler.CreateMd5Sign("key", wxPayConfig.MchKey));//商户API密钥
            #endregion
            //将参数转为xml字符串
            string data = packageReqHandler.ParseXML();
            var result = await HttpRequestHelper.PostRequestAsync(new Uri("https://api.mch.weixin.qq.com/pay/unifiedorder"), data);
            var res = System.Xml.Linq.XDocument.Parse(result);
            try
            {
                string mweb_url = res.Element("xml").Element("mweb_url").Value;
                //Log.Info("【GetWxH5PayUrl】订单：" + out_trade_no + ",请求得到的url：" + mweb_url, "微信支付");
                return mweb_url;
            }
            catch (Exception ex)
            {
                //Log.Info("【GetWxH5PayUrl】订单：" + out_trade_no + ",异常:" + ex.ToString(), "微信支付");
                return ex.ToString();
            }
        }


    }
}
